#include "Rp2Triangle3.h"

using namespace Rp2;

//---------------------------------------------------------------------------------------------------
Triangle3::Triangle3()
{
	// uninitialized
}
//---------------------------------------------------------------------------------------------------
Triangle3::Triangle3(const Vector3f& rkV0, const Vector3f& rkV1, const Vector3f& rkV2)
{
	V[0] = rkV0;
	V[1] = rkV1;
	V[2] = rkV2;
}
//---------------------------------------------------------------------------------------------------
Triangle3::Triangle3(const Vector3f akV[3])
{
	V[0] = akV[0];
	V[1] = akV[1];
	V[2] = akV[2];
}
//---------------------------------------------------------------------------------------------------
float Triangle3::DistanceTo(const Vector3f& rkQ) const
{
	Vector3f kDiff = V[0] - rkQ;
	Vector3f kE0 = V[1] - V[0], kE1 = V[2] - V[0];
	float fA00 = kE0.SquaredLength();
	float fA01 = kE0.Dot(kE1);
	float fA11 = kE1.SquaredLength();
	float fB0 = kDiff.Dot(kE0);
	float fB1 = kDiff.Dot(kE1);
	float fC = kDiff.SquaredLength();
	float fDet = fabs(fA00*fA11-fA01*fA01);
	float fS = fA01*fB1 - fA11*fB0;
	float fT = fA01*fB0 - fA00*fB1;
	float fSqrDist;

    if (fS + fT <= fDet)
    {
        if (fS < 0.0f)
        {
            if (fT < 0.0f)  // region 4
            {
                if (fB0 < 0.0f)
                {
                    if (-fB0 >= fA00)
                    {
                        fSqrDist = fA00+(2.0f)*fB0+fC;
                    }
                    else
                    {
                        fSqrDist = fC-fB0*fB0/fA00;
                    }
                }
                else
                {
                    if (fB1 >= 0.0f)
                    {
                        fSqrDist = fC;
                    }
                    else if (-fB1 >= fA11)
                    {
                        fSqrDist = fA11+(2.0f)*fB1+fC;
                    }
                    else
                    {
                        fSqrDist = fC-fB1*fB1/fA11;
                    }
                }
            }
            else  // region 3
            {
                if (fB1 >= 0.0f)
                {
                    fSqrDist = fC;
                }
                else if (-fB1 >= fA11)
                {
                    fSqrDist = fA11+(2.0f)*fB1+fC;
                }
                else
                {
                    fSqrDist = fC-fB1*fB1/fA11;
                }
            }
        }
        else if (fT < 0.0f)  // region 5
        {
            if (fB0 >= 0.0f)
            {
                fSqrDist = fC;
            }
            else if (-fB0 >= fA00)
            {
                fSqrDist = fA00+(2.0f)*fB0+fC;
            }
            else
            {
                fSqrDist = fC-fB0*fB0/fA00;
            }
        }
        else  // region 0
        {
            // minimum at interior point
            float fInvDet = 1.0f/fDet;
            fS *= fInvDet;
            fT *= fInvDet;
            fSqrDist = fS*(fA00*fS + fA01*fT + 2.0f*fB0) +
                fT*(fA01*fS + fA11*fT + 2.0f*fB1)+fC;
        }
    }
    else
    {
        float fTmp0, fTmp1, fNumer, fDenom;

        if (fS < 0.0f)  // region 2
        {
            fTmp0 = fA01 + fB0;
            fTmp1 = fA11 + fB1;
            if (fTmp1 > fTmp0)
            {
                fNumer = fTmp1 - fTmp0;
                fDenom = fA00-2.0f*fA01+fA11;
                if (fNumer >= fDenom)
                {
                    fSqrDist = fA00 + 2.0f*fB0 + fC;
                }
                else
                {
                    fS = fNumer/fDenom;
                    fT = 1.0f - fS;
                    fSqrDist = fS*(fA00*fS+fA01*fT+2.0f*fB0) +
                        fT*(fA01*fS + fA11*fT + 2.0f*fB1)+fC;
                }
            }
            else
            {
                if (fTmp1 <= 0.0f)
                {
                    fSqrDist = fA11 + 2.0f*fB1 + fC;
                }
                else if (fB1 >= 0.0f)
                {
                    fSqrDist = fC;
                }
                else
                {
                    fSqrDist = fC-fB1*fB1/fA11;
                }
            }
        }
        else if (fT < 0.0f)  // region 6
        {
            fTmp0 = fA01 + fB1;
            fTmp1 = fA00 + fB0;
            if (fTmp1 > fTmp0)
            {
                fNumer = fTmp1 - fTmp0;
                fDenom = fA00 - 2.0f*fA01 + fA11;
                if (fNumer >= fDenom)
                {
                    fT = 1.0f;
                    fS = 0.0f;
                    fSqrDist = fA11 + 2.0f*fB1 + fC;
                }
                else
                {
                    fT = fNumer/fDenom;
                    fS = 1.0f - fT;
                    fSqrDist = fS*(fA00*fS+fA01*fT + 2.0f*fB0) +
                        fT*(fA01*fS+fA11*fT+2.0f*fB1)+fC;
                }
            }
            else
            {
                if (fTmp1 <= 0.0)
                {
                    fSqrDist = fA00 + 2.0f*fB0 + fC;
                }
                else if (fB0 >= 0.0f)
                {
                    fSqrDist = fC;
                }
                else
                {
                    fSqrDist = fC-fB0*fB0/fA00;
                }
            }
        }
        else  // region 1
        {
            fNumer = fA11 + fB1 - fA01 - fB0;
            if (fNumer <= 0.0f)
            {
                fSqrDist = fA11 + 2.0f*fB1 + fC;
            }
            else
            {
                fDenom = fA00-2.0f*fA01+fA11;
                if (fNumer >= fDenom)
                {
                    fSqrDist = fA00 + 2.0f*fB0 + fC;
                }
                else
                {
                    fS = fNumer/fDenom;
                    fT = 1.0f - fS;
                    fSqrDist = fS*(fA00*fS + fA01*fT + 2.0f*fB0) +
                        fT*(fA01*fS + fA11*fT + 2.0f*fB1)+fC;
                }
            }
        }
    }

	return (float)sqrt(fabs(fSqrDist));
}
//---------------------------------------------------------------------------------------------------